/**
 */
package com.dingpeikeji.quickdeer.core.modules.vehi.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.driver.service.TDriversService;
import com.dingpeikeji.quickdeer.core.modules.vehi.dao.TRealscheVehisDao;
import com.dingpeikeji.quickdeer.core.modules.vehi.entity.TRealscheVehis;
import com.dingpeikeji.quickdeer.modules.sms.entity.Sms;
import com.dingpeikeji.quickdeer.modules.sms.service.SmsApi;

/**
 * 车辆调度Service
 * @author Zhaoy
 * @version 2016-07-01
 */
@Service
@Transactional(readOnly = true)
public class TRealscheVehisService extends CrudService<TRealscheVehisDao, TRealscheVehis> {
	@Autowired
	private TDriversService drvService;

	public TRealscheVehis get(String id) {
		return super.get(id);
	}
	
	public List<TRealscheVehis> findList(TRealscheVehis tRealscheVehis) {
		return super.findList(tRealscheVehis);
	}
	
	public Page<TRealscheVehis> findPage(Page<TRealscheVehis> page, TRealscheVehis tRealscheVehis) {
		if(StringUtils.isEmpty(tRealscheVehis.getDepartdateStr())){
			tRealscheVehis.setDepartdateStr(DateUtils.getDate("yyyy-MM-dd").toString()); // 查询当天的
		}
		return super.findPage(page, tRealscheVehis);
	}
	
	public Page<TRealscheVehis> findOrderPage(Page<TRealscheVehis> page, TRealscheVehis tRealscheVehis) {
		if(StringUtils.isEmpty(tRealscheVehis.getDepartdateStr())){
			tRealscheVehis.setDepartdateStr(DateUtils.getDate("yyyy-MM-dd").toString()); // 查询当天的
		}
		tRealscheVehis.setPage(page);
		page.setOrderBy("b.departdate");
		page.setList(dao.findOrderList(tRealscheVehis));
		return page;
	}
		
	@Transactional(readOnly = false)
	public void save(TRealscheVehis tRealscheVehis) {
		super.save(tRealscheVehis);
	}
	
	@Transactional(readOnly = false)
	public void confirm(TRealscheVehis tRealscheVehis) {
		//保存状态
		super.save(tRealscheVehis);
		//通知客户
		if (!tRealscheVehis.getOrders().getPayertel().equals("")){
			Sms sendSms = new Sms();
			TDrivers tmpDrivers = drvService.get(tRealscheVehis.getMaindriver());
			sendSms.setMobile(tRealscheVehis.getOrders().getPayertel());
			sendSms.setContent("尊敬的"+tRealscheVehis.getOrders().getContacts()+
						"，您在霞客约车购买的用车服务已经派单，车牌号为："+tRealscheVehis.getLicno()+
						"，请及时与驾驶员"+tmpDrivers.getDrvname() +
						"师傅联系，手机号为"+tmpDrivers.getMobile()+
						"，请按照预定时间到达上车地点。祝您出行愉快！");
			try {
				SmsApi.sendSms(sendSms);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TRealscheVehis tRealscheVehis) {
		super.delete(tRealscheVehis);
	}
	
}
